<?php

abstract class WebController extends AuthorizeWebController implements IForwarder {
    
    protected $contextPath;
    protected $language;
    protected $cache;
    protected $orm_builder;
   
    public function doAction(HttpRequest $request,HttpResponse $response){
        $this->contextPath = APPLICATION_URL."/".$request->get("_LANGUAGE",DEFAULT_LANGUAGE);
        $this->language = $request->get("_LANGUAGE",DEFAULT_LANGUAGE);
        $this->cache = array();
        $this->orm_builder = new ORMBuilder(RENZA_DBMS);
        if($this->isAuthorize($request)){
            $this->processRequest($request->get("_ACTION",DEFAULT_CONTROLLER_ACTION),$request,$response);
            $this->display($request,$response);
        }
    }
    
    protected abstract function processRequest($action_type,HttpRequest $request,HttpResponse $response);
    
    protected abstract function display(HttpRequest $request,HttpResponse $response);
    
    public function sendRedirect($page,  HttpResponse $response){
        $response->sendRedirect("/".$this->language.$page);
    }
    
    public function forward($page,HttpRequest $request){
        global $_LAYOUTS;
        //extension controller============================
        if($_LAYOUTS[$page] == "tpl1.html"){
            $forwarder = new Tpl1Forwarder($this,$this->cache);
        }else if($_LAYOUTS[$page] == "tpl2.html"){
            $forwarder = new Tpl2Forwarder($this,$this->cache);
        }else if($_LAYOUTS[$page] == "tpl3.html"){
            $forwarder = new Tpl3Forwarder($this,$this->cache);
        }
        //================================================
        $forwarder->execForward($page,$request);
    }
    
    public function execForward($page,  HttpRequest $request){
        global $_LAYOUTS;
        SmartyOwner::getInstance()->configDir(TEMPLATE_DIR,TEMPLATE_COMPILE_DIR
                , TEMPLATE_DIR);
        
        SmartyOwner::configLoad(LANGUAGE_DIR."tpl"."/".$_LAYOUTS[$page].".ini",$this->language);
        SmartyOwner::configLoad(LANGUAGE_DIR.$page.".ini",$this->language);
        $request->setAttribute("_ERROR_CODE",$this->getError());
        $request->setAttribute("_LANGUAGE",$this->language);
        $request->setAttribute("_CONTEXT_PATH",$this->contextPath);
        $request->setAttribute("_CACHE",$this->cache);
        $request->setAttribute("_PAGE_FILE",$page);
        $request->setAttribute("_REQUEST",$request);
        $request->forward($_LAYOUTS[$page]);
    }
}

